package com.hq.lib_comview.common.utils;

import android.text.format.Time;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 描述 ：日期操作工具
 * 作者 ：hq
 * 时间 ：2017/8/7 10:26
 */
public class TimeUtils {

    /**
     * 返回当前系统时间
     */
    public static String getDataTime(String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(new Date());
    }

    /**
     * 判断当前日期是星期几
     *
     * @param pTime 设置的需要判断的时间 时间戳
     * @return dayForWeek 判断结果
     * @Exception 发生异常
     */
//  String pTime = "2012-03-12";
    public static String getWeek(String pTime) {
        String Week = "星期";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(pTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            Week += "天";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
            Week += "一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
            Week += "二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
            Week += "三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
            Week += "四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
            Week += "五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            Week += "六";
        }
        return Week;
    }

    /**
     * 判断当前日期是否是今天
     *
     * @param when 时间戳
     * @return
     */
    public static boolean isToday(String when) {
        if (StringUtils.isEmpty(when)) {
            return false;
        }
        long times = 0;
        try {
            times = Long.valueOf(when);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Time time = new Time();
        time.set(times);
        int thenYear = time.year;
        int thenMonth = time.month;
        int thenMonthDay = time.monthDay;

        time.set(System.currentTimeMillis());
        Log.d("test", "isToday when " + when + "  timeStamp" + time.toString());
        return (thenYear == time.year)
                && (thenMonth == time.month)
                && (thenMonthDay == time.monthDay);
    }

    /**
     * 判断是否是昨天
     *
     * @param when 时间戳
     * @return
     */
    public static boolean isYesterday(String when) {
        if (StringUtils.isEmpty(when)) {
            return false;
        }
        long times = 0;
        try {
            times = Long.valueOf(when);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Time time = new Time();
        time.set(times);

        int thenYear = time.year;
        int thenMonth = time.month;
        int thenMonthDay = time.monthDay;

        time.set(System.currentTimeMillis());
        return (thenYear == time.year)
                && (thenMonth == time.month)
                && (thenMonthDay == time.monthDay - 1);
    }

    /**
     * 判断是否是今年
     *
     * @param when 时间戳
     * @return
     */
    public static boolean isToyear(String when) {
        if (StringUtils.isEmpty(when)) {
            return false;
        }
        long times = 0;
        try {
            times = Long.valueOf(when);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Time time = new Time();
        time.set(times);

        int thenYear = time.year;

        time.set(System.currentTimeMillis());
        return (thenYear == time.year);
    }

    /*
    * 将时间戳转换为时间
    * @param s 时间戳
    * @param formart 格式
    */
    public static String stampToDate(String s, String formart) {
        if (StringUtils.isEmpty(s)) {
            return "";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formart);
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }


}
